import axios from 'axios';
import { Message,Loading } from "element-ui"
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    timeout: 5000
});
let that = this

var loadingInstance =''
service.interceptors.request.use(
    config => {
		 loadingInstance = Loading.service({
		      lock: true,
			  text: 'Loading',
			  spinner: 'el-icon-loading',
			  background: 'rgba(0, 0, 0, 0.7)'	
		});
		console.log(111,config)
		config.headers.Authorization=window.localStorage.getItem('Authorization')
		// config.headers.token=window.localStorage.getItem('Authorization')
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);
// if(localStorage.getItem(''))

// service.interceptors.request.headers.set('token', window.localStorage.getItem('token'))
service.interceptors.response.use(
    response => {
	//	that.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
		  loadingInstance.close();
		//});
		if(response.data.code==500){
			Message.error(response.data.msg)
		}
		if (response.data.code == 401) {
		    localStorage.clear()
			location.reload() 
		}
        if (response.status === 200) {
            return response.data;
			console.log(111)
        } else {
			console.log(2222)
            Promise.reject();
        }
		
		
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
